package com.lanou.sinasports.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.sinasports.R;
import com.lanou.sinasports.activity.NewsRecDetOneActivity;
import com.lanou.sinasports.activity.NewsRecTwoActivity;
import com.lanou.sinasports.beans.NewRecom;
import com.lanou.sinasports.utils.BitmapCache;
import com.lanou.sinasports.utils.DoubleCache;
import com.lanou.sinasports.utils.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/15.
 */
public class NewRecomAdapter extends BaseAdapter {
    private Context context;
    private List<NewRecom> data;
    private RequestQueue queue;
    private ImageLoader imageLoader;


    private static final int typeCount=2;
    private static final int typeOne=0;
    private static final int typeTwo=1;

    private int currentType;



    public NewRecomAdapter(Context context, List<NewRecom> data) {
        this.context = context;
        this.data = data;
        queue=Volley.newRequestQueue(context);
        imageLoader=new ImageLoader(queue,new DoubleCache());
    }


    public void addData(List<NewRecom> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        currentType=getItemViewType(position);
        if(currentType==typeOne){
            NewRecOneViewHolder oneViewHolder;
            if(convertView==null){
                convertView= LayoutInflater.from(context).inflate(R.layout.item_newsrecommend1,parent,false);
                oneViewHolder=new NewRecOneViewHolder(convertView);

                convertView.setTag(oneViewHolder);

            }else {
                oneViewHolder= (NewRecOneViewHolder) convertView.getTag();
            }
            oneViewHolder.tvTitle.setText(data.get(position).getStitle());
            oneViewHolder.tvCount.setText(data.get(position).getComment_total() + "评");

            oneViewHolder.ivone.setDefaultImageResId(R.mipmap.ic_myteam_sethost);
            oneViewHolder.ivone.setErrorImageResId(R.mipmap.ic_main_tab_news_p);
            oneViewHolder.ivone.setImageUrl(data.get(position).getImages(), imageLoader);

            oneViewHolder.ivtwo.setDefaultImageResId(R.mipmap.ic_myteam_sethost);
            oneViewHolder.ivtwo.setErrorImageResId(R.mipmap.ic_main_tab_news_p);
            oneViewHolder.ivtwo.setImageUrl(data.get(position).getImages(), imageLoader);

            oneViewHolder.ivthree.setDefaultImageResId(R.mipmap.ic_myteam_sethost);
            oneViewHolder.ivthree.setErrorImageResId(R.mipmap.ic_main_tab_news_p);
            oneViewHolder.ivthree.setImageUrl(data.get(position).getImages(), imageLoader);

            oneViewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, NewsRecTwoActivity.class);
                    intent.putExtra("url",data.get(position).getUrl());
                    context.startActivity(intent);

                }
            });


        }else if(currentType==typeTwo){
            final NewRecTwoViewHolder twoViewHolder;

            if(convertView==null){
                Log.e("xxxx", "--------> jinru");
                convertView=LayoutInflater.from(context).inflate(R.layout.item_newsrecommend2,parent,false);
                twoViewHolder=new NewRecTwoViewHolder(convertView);
                convertView.setTag(twoViewHolder);
            }else {
                twoViewHolder= (NewRecTwoViewHolder) convertView.getTag();
            }

            twoViewHolder.tvTitle.setText(data.get(position).getStitle());
            twoViewHolder.tvSummary.setText(data.get(position).getWapsummary());
            twoViewHolder.tvCount.setText(data.get(position).getComment_total() + "评");

            twoViewHolder.imageView.setDefaultImageResId(R.mipmap.bg_personal_sc_header);
            twoViewHolder.imageView.setErrorImageResId(R.mipmap.ic_main_tab_match);
            twoViewHolder.imageView.setImageUrl(data.get(position).getImages(), imageLoader);


            twoViewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, NewsRecDetOneActivity.class);
                    intent.putExtra("position", position);

                    intent.putExtra("img", data.get(position).getImages());

                    intent.putExtra("ur", data.get(position).getUrl());

                    context.startActivity(intent);
                }
            });
        }

        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        Log.d("NewRecomAdapter", data.get(position).getCategoryid());
        if (data.get(position).getCategoryid().equals("2")) {

            return typeOne;
        } else {
            return typeTwo;
        }

    }

    @Override
    public int getViewTypeCount() {

        return 2;
    }

    class NewRecOneViewHolder{
        TextView tvTitle;
        TextView tvCount;
        NetworkImageView ivone;
        NetworkImageView ivtwo;
        NetworkImageView ivthree;
        LinearLayout linearLayout;
        public NewRecOneViewHolder(View view) {
            tvTitle= (TextView) view.findViewById(R.id.tv_item_newsrecommend1_title);
            tvCount= (TextView) view.findViewById(R.id.tv_item_newsrecommend1_count);
            ivone= (NetworkImageView) view.findViewById(R.id.iv_item_newsrecomend1_1);
            ivtwo= (NetworkImageView) view.findViewById(R.id.iv_item_newsrecomend1_2);
            ivthree= (NetworkImageView) view.findViewById(R.id.iv_item_newsrecomend1_3);
            linearLayout= (LinearLayout) view.findViewById(R.id.ll_newsrec1);
        }
    }

    class NewRecTwoViewHolder{
        NetworkImageView imageView;
        TextView  tvTitle;
        TextView  tvSummary;
        TextView  tvCount;
        LinearLayout linearLayout;
        public NewRecTwoViewHolder(View view) {
             imageView= (NetworkImageView) view.findViewById(R.id.iv_item_newrecommend2);
             tvTitle= (TextView) view.findViewById(R.id.tv_item_newsrecommend2_stitle);
             tvSummary= (TextView) view.findViewById(R.id.tv_item_newsrecommend2_summary);
             tvCount= (TextView) view.findViewById(R.id.tv_item_newsrecommend2_count);
             linearLayout= (LinearLayout) view.findViewById(R.id.ll_newsrec2);
        }
    }



}
